<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo 用户表
 *@link www.zeevin.com
 */
class User extends CActiveRecord{

	public $old_email;
	public $new_email;
	public $repassword_tmp;
	public $password_tmp;
	const PAGE_SIZE=16;

	//用户状态
    const STAFF_STATUS_NORMAL = 0;
    const STAFF_STATUS_DISABLE = 1;
    const STAFF_STATUS_LEAVING = 2;

    //收信后邮箱提醒设置
    const ALERT_SETTING_NORMAL = 0;
    const ALERT_SETTING_T = 3;
    const ALERT_SETTING_S = 7;
    const ALERT_SETTING_FT = 15;
    const ALERT_SETTING_NONE = 1;

    //个人空间隐私设置
    const SPACE_PRIVACY_PUBLIC=0;
    const SPACE_PRIVACY_LOGIN=1;
    const SPACE_PRIVACY_FRIEND=2;

    //垃圾箱设置
    const SPAM_SETTING_O=1;
    const SPAM_SETTING_T=3;
    const SPAM_SETTING_F=5;
    const SPAM_SETTING_NONE=0;

    //性别
    const SEX_NONE=0;
    const SEX_MALE=1;
    const SEX_FEMAL=2;

    //学历
    const DEGREE_NONE=0;
    const DEGREE_PRIMARY=1;
    const DEGREE_JUNIOR=2;
    const DEGREE_TECHNICAL=3;
    const DEGREE_SENIOR=4;
    const DEGREE_COLLEGE=5;
    const DEGREE_BACHELOR=6;
    const DEGREE_MASTER=7;
    const DEGREE_DOCTOR=8;

    //收入水平
    const INCOME_NONE=0;
    const INCOME_ONE=1;
    const INCOME_TWO=2;
    const INCOME_THREE=3;
    const INCOME_FOUR=4;
    const INCOME_FIVE=5;
    const INCOME_SIX=6;
    const INCOME_SEVEN=7;
    const INCOME_EIGHT=8;
    const INCOME_NINE=9;

	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function tableName(){
		return 'users';
	}


	public function rules(){
		return array(
			array('old_email,new_email,sex,province_id,city_id,edu,income,height', 'safe'),
            array('old_email,new_email,email ', 'email','message'=>'邮箱格式不正确'),
			array('alert_setting,spam_setting,space_privacy,status','numerical'),
			array('nickname','length','min'=>3,'max'=>15,'tooLong'=>'最长15个字符','tooShort'=>'最少3个字符'),
			array('subdomain','length','min'=>5,'max'=>15,'tooLong'=>'最长15个字符','tooShort'=>'最少5个字符'),
			array('nickname,email,password_tmp,subdomain,birth','required','message'=>'不能为空'),
			array('password_tmp','length','min'=>5,'max'=>20,'tooLong'=>'最长20个字符','tooShort'=>'最少5个字符'),
			array('email,subdomain,nickname','unique','message'=>'已经被占用'),
			array('subdomain','match','pattern'=>'/^[A-Z0-9][A-Z0-9_-]+$/i','message'=>'最少5个字符，只能含有字母数字或下划线'),
			array('birth','date','format'=>'yyyy-MM-dd','message'=>'格式不正确'),
			array('avatar', 'file','types'=>'jpg, gif, png, jpeg, bmp','maxSize'=>1048576,'tooLarge'=>'头像不能超过2M', 'allowEmpty'=>false, 'on'=>'insert','message'=>'图片格式不正确','wrongType'=>'图片格式不正确只允许：jpg, gif, png, jpeg, bmp类型的图片'),
		);
	}

	protected function beforeSave(){
		if($this->isNewRecord){
			$this->regdate = date('Y-m-d H:i:s');
			
		}
		return ture;
	}

	//根据用户id获取用户信息
	public static function getUserById($id){
		$rs = User::model()->findByPk($id);
		return isset($rs)?$rs:'无';
	}
	public static function getAlert($a=null){
		$alerts = array(
			self::ALERT_SETTING_NORMAL=>'收到信件，即发邮件提醒',
			self::ALERT_SETTING_T=>'3天未登录，若有信件发邮件提醒',
			self::ALERT_SETTING_S=>'7天未登录，若有信件发邮件提醒',
			self::ALERT_SETTING_FT=>'15天未登录，若有信件发邮件提醒',
			self::ALERT_SETTING_NONE=>'不提醒'
			);
		return isset($a)?$alerts[$a]:$alerts;
	}


	public static function getPrivacy($p=null){
		$privacys = array(
			self::SPACE_PRIVACY_PUBLIC=>'对外显示',
			self::SPACE_PRIVACY_LOGIN=>'只对登陆用户（不包括黑名单）显示',
			self::SPACE_PRIVACY_FRIEND=>'只对自己关注的人显示'
			
			);
		return isset($p)?$privacys[$p]:$privacys;
	}

	public static function getSpam($s=null){
		$spams = array(
			self::SPAM_SETTING_O=>'低于1个',
			self::SPAM_SETTING_T=>'低于3个',
			self::SPAM_SETTING_F=>'低于5个',
			self::SPAM_SETTING_NONE=>'不限制'
			);
		return isset($s)?$spams[$s]:$spams;
	}

	public static function getSex($s=null){
		$sexs = array(
			self::SEX_NONE=>'不限',
			self::SEX_MALE=>'男',
			self::SEX_FEMAL=>'女'
			);
		return isset($s)?$sexs[$s]:$sexs;

	}

	public static function getDegree($d=null){
		$degrees = array(
			self::DEGREE_NONE=>'无',
			self::DEGREE_PRIMARY=>'小学',
			self::DEGREE_JUNIOR=>'初中',
			self::DEGREE_TECHNICAL=>'中专',
			self::DEGREE_SENIOR=>'高中',
			self::DEGREE_COLLEGE=>'大专',
			self::DEGREE_BACHELOR=>'本科',
			self::DEGREE_MASTER=>'硕士',
			self::DEGREE_DOCTOR=>'博士'
			);
		return isset($d)?$degrees[$d]:$degrees;
	}

	public static function getIncome($i=null){
		$income = array(
			self::INCOME_NONE =>'无',
			self::INCOME_ONE=>'1000元以下',
			self::INCOME_TWO=>'1001到2000元',
			self::INCOME_THREE=>'2001到3001元',
			self::INCOME_FOUR=>'3001到5000元',
			self::INCOME_FIVE=>'5001到8000元',
			self::INCOME_SIX=>'8001到10000元',
			self::INCOME_SEVEN=>'10001到20000元',
			self::INCOME_EIGHT=>'20001到50000元',
			self::INCOME_NINE=>'50000以上'
			);
		return isset($i)?$income[$i]:$income;
	}

	public static function getList($param = array()){
		$condition = '1=1 ';

		if(in_array($param['sex_tendency'],array(User::SEX_FEMAL,User::SEX_MALE))){
			$condition .=' AND sex=:sex';
			$params['sex']=$param['sex_tendency'];
		}
		
		if(in_array($param['age_start'],range(18, 80))){
			$condition .=' AND birth<=:age_start';
			$params['age_start']=Utils::getBirth($param['age_start']);
		}
		if(in_array($param['age_end'],range(18, 80))){
			$condition .=' AND birth>=:age_end';
			$params['age_end']=Utils::getBirth($param['age_end']);
		}
		

		if($param['space_privacy']){
			$condition .=' AND space_privacy=:space_privacy';
			$params['space_privacy']=$param['space_privacy'];
		}
		
		if($param['edu_tendency']){
			$condition .=' AND edu=:edu_tendency';
			$params['edu_tendency']=$param['edu_tendency'];
		}

		if($param['income_tendency']){
			$condition .=' AND income=:income_tendency';
			$params['income_tendency']=$param['income_tendency'];
		}
		
		if($param['height']){
			$condition .=' AND height>=:height';
			$params['height']=$param['height'];
		}
		// print_r($params);exit;
		$criteria=new CDbCriteria();
	

		$criteria->condition = $condition;
		$criteria->params = $params;
		$criteria->select = 'id,nickname,sex,birth,city_id,avatar';
		$criteria->order = 'id DESC';
    	$count=User::model()->count($criteria);
    	$pages=new CPagination($count);

    	// results per page
   	 	$pages->pageSize=self::PAGE_SIZE;
    	$pages->applyLimit($criteria);
    	$rs['total_num'] = $count;
    	$rs['rows'] = User::model()->findAll($criteria);
    	$rs['pages'] = $pages;
    	return $rs;
	}

}